package com.akk.module.avs.service;

import com.akk.module.avs.entity.AvsYbLoanExample;
import com.akk.module.avs.entity.AvsYbLoanType;
import com.akk.module.avs.entity.AvsYbLoanTypeExample;
import com.akk.module.avs.mapper.AvsYbLoanMapper;
import com.akk.module.avs.mapper.AvsYbLoanTypeMapper;
import com.akk.module.avs.mapper.dao.AvsYbLoanRecordDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AvsYbLoanTypeService {

    @Autowired
    private AvsYbLoanTypeMapper ybLoanTypeMapper;

    @Autowired
    private AvsYbLoanTypeExample ybLoanTypeExample;

    @Autowired
    private AvsYbLoanMapper loanMapper;

    @Autowired
    private AvsYbLoanExample loanExample;

    @Autowired
    private AvsYbLoanRecordDao recordDao;

    public List<AvsYbLoanType> findAll(String appId){
        ybLoanTypeExample.clear();
        ybLoanTypeExample.createCriteria().andLoanTypeAppIdEqualTo(appId);
        return ybLoanTypeMapper.selectByExample(ybLoanTypeExample);
    }

    public AvsYbLoanType findById(Long typeId){
        return ybLoanTypeMapper.selectByPrimaryKey(typeId);
    }

    public void save(AvsYbLoanType type){
        ybLoanTypeMapper.insert(type);
    }

    public void update(AvsYbLoanType type){
        ybLoanTypeMapper.updateByPrimaryKey(type);
    }

    @Transactional
    public void delete(Long typeId){
        recordDao.deleteByLoanTypeId(typeId);
        loanExample.clear();
        loanExample.createCriteria().andLoanTypeIdEqualTo(typeId.longValue());
        loanMapper.deleteByExample(loanExample);
        ybLoanTypeMapper.deleteByPrimaryKey(typeId);
    }

}
